<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      >
<head th:replace="fragments/tabhead::headc"></head>

<body>

<th:block layout:fragment="content">
<!-- 销售订单 -->
<div style="margin-top:15px">
  <form class="layui-form search" id="searchForm">
  <div class="layui-form-item layui-elem-quote">
     <div class="layui-inline">
      <label class="layui-form-label" style="width:100px;margin-left:-20px">所属公司</label>
      <div class="layui-input-inline">
        <select name="companyId" id="companyId"><option value="1">深圳企智达有限公司</option></select>
      </div>
    </div>
    <div class="layui-inline">
      <label class="layui-form-label" style="width:80px;margin-left:-20px">供应商</label>
      <div class="layui-input-inline" style="width:100px">
        <input type="tel" name="customerName" class="layui-input" placeholder="请输入名称"/>
      </div>
    </div>
   <div class="layui-inline">
      <label class="layui-form-label" style="width:100px;margin-left:-20px">审核状态</label>
      <div class="layui-input-inline" style="width:100px;">
        <select name="reviewStatus">
        	<option value="">请选择审核状态</option>
        	<option th:each="s : ${status}" th:attr="value=${s.code}" th:text="${s.value}"></option>
        </select>
      </div>
    </div>
    <div class="layui-inline">
      <label class="layui-form-label" style="width:60px;margin-left:-20px">时间</label>
      <div class="layui-input-inline" style="width:100px">
        <input type="text" id="startDate" class="layui-input" name='startDate'/>
      </div>
       <div class="layui-form-mid">-</div>
      <div class="layui-input-inline" style="width:100px">
        <input type="text" id="endDate" class="layui-input" name="endDate"/>
      </div>
    </div>
	
    <div class="layui-inline">
      <div class="layui-input-inline" style="width:150px;">
        <button class="layui-btn" id="searchBtn" type="button">查询</button>
        <button class="layui-btn" id="addBtn" type="button" authcode='900031'>新增</button>
      </div>
    </div>
    
  </div>
 </form>
</div>

<table class="layui-table"  id="tab" lay-filter="tab"></table>
<script>
var ins,page,trobj,editdialogindex,table;
$(function(){ 
	
	layui.use(['table','layer'], function(){
		table = layui.table;
		ins =table.render({ //其它参数在此省略
		 elem:"#tab",
		 url: '/stock/inbound/data?first=true',
		 cols:[[
			  {field:'id',width:80, sort: true,title:'ID',fixed:'true'},
		      {field:'companyName', width:180,title:'所属公司'},
		      {field:'sourceOrderNo', width:80, sort: true,title:'来源单号'},
		      {field:'orderNo', width:180,title:'订单编号'},
		      {field:'supplierName',width:177,title:'供应商名称'},
		      {field:'supplierConnact', width:80, sort: true,title:'供应商联系人'},
		      {field:'phone', width:80, sort: true,title:'联系电话'},
		      {field:'count', width:135, sort: true,title:'订单总量'},
		      {field:'status', width:135, sort: true,title:'状态',templet:'#statusTpl'},
		      
		      {field:'statementName', width:135, sort: true,title:'结算方式'},
		      {field:'statementDays', width:135, sort: true,title:'结算天数'},
		      {field:'currencyName', width:135, sort: true,title:'结算币种'},
		      {field:'taxRate', width:135, sort: true,title:'税率',templet:'#taxTpl'},
		      {field:'exchangeRate', width:135, sort: true,title:'汇率'},
		      
		      
		      {field:'confirmPerson',width:177,title:'审核人'},
		      {field:'confirmDate', width:80, sort: true,title:'审核日期'},
		      {field:'modifyPerson', width:80, sort: true,title:'修改人'},
		      {field:'mofifyDate', width:80,title:'修改日期'},
		      {field:'createPerson', width:135, sort: true,title:'创建人'},
		      {field:'createDate', width:135, sort: true,title:'创建日期'},
		      {field:'remark', width:135, sort: true,title:'备注'},
		      {align:'center',fixed:'right',width:300, toolbar:'#toolbar',title:'操作'}
		 ]]
	    ,page: true //是否显示分页
	    ,limits: [10,20,50]
	    ,limit:10 //每页默认显示的数量
		  //where:{phone:phone}
		  //method: 'post'
		 ,done:function(res,curr,count){
			 console.log(res);
			 page = curr;
			    //得到当前页码
			    console.log(curr); 
			    
			    //得到数据总量
			    console.log(count);
		 }
		});
		
		table.on("tool(tab)",function(obj){
			var data = obj.data; //获得当前行数据
			var layEvent = obj.event; //获得 lay-event 对应的值
			trobj = obj;
			var tr = obj.tr; //获得当前行 tr 的DOM对象
			if(layEvent=='edit'){
				$.post('/stock/inbound/edit?id='+data.id, {}, function(str){
				editdialogindex = layer.open({
									title:"修改采购入库单",
								    type: 1,
								    area: ['800px', '500px'],
								    content: str, //注意，如果str是object，那么需要字符拼接。
								    zIndex:10
								  });
								});
			}else if(layEvent=='del'){
				/* if(data.status == 1) {
					layer.alert("单据已审核，不能被删除！！", {icon: 4})
				}else{
					$.post('/stock/inbound/del/'+data.id,{},function(data) {
						if(data.code == 200) {
							layer.msg("单据删除成功");
							ins.reload(where{
								
							});
						}else{
							layer.msg(data.message);
						}
					});	 */	
				    layer.confirm('确定删除该行单据', {icon: 3, title:'提示'}, function(index){
				    	if(data.status == 1) {
				    		layer.msg("单据已审核，不能被删除！！", {icon: 4});
				    		layer.close(index);
				    	}else{
				    		 //向服务端发送删除指令
				    		$.post('/stock/inbound/del?id='+data.id,{},function(result) {
				    			if(result.code == 200) {
				    				trobj.del(); //删除对应行（tr）的DOM结构
								    layer.close(index);
								    layer.msg(result.message,{icon: 6});
								    return false;
				    			}else{
				    			 layer.msg(result.message,{icon: 0});
				    			 layer.close(index);
				    			 return false;
				    			} 
				    		});
				    	}
				      
				      });
					return false;
			}else if(layEvent=="close"){
				console.log(data.status);				
				layer.confirm('确定关闭这条单据', {icon: 3, title:'提示'},function(index) {
					if(data.status == 2) {
			    		layer.msg("单据已关闭！！", {icon: 4});
			    		layer.close(index);
			    	}else{
			    		 //向服务端发送关闭指令
			    		$.post('/stock/inbound/close?id='+data.id,{},function(result) {
			    			if(result.code == 200) {
			    				//obj.del(); //删除对应行（tr）的DOM结构
			    				/* obj.update({
									status:0			    				
			    				}); */
							    layer.msg(result.message,{icon: 6});
// 							    ins.reload({
// 			           	    		url:'/stock/inbound/list',
// 			           	    		where: {
// 			           	    			companyId: '1'
// 			           	    			//reviewStatus: '0'
// 			           	    		}
// 			           	    	});
								 reloadData();
								layer.close(index);
							    return false;
			    			}else{
			    			 layer.msg(result.message,{icon: 2});
			    			 return false;
			    			} 
			    		});
			    	}
				});
				return false;
			}else if(layEvent=="view") {
					$.post('/stock/inbound/view/'+data.id,{},function(str) {
						editdialogindex = layer.open({
							title:"采购入库单查看",
						    type: 1,
						    area: ['800px', '450px'],
						    content: str, //注意，如果str是object，那么需要字符拼接。
						    zIndex:10
						  });
						});
					return false;
			}else if(layEvent=="celview") {
				console.log(data.status);
				layer.confirm('确定弃审这条单据', {icon: 3, title:'提示'},function(index) {
					if(data.status == 0) {
			    		layer.msg("单据未审核，不能弃审！！", {icon: 4});
			    		layer.close(index);
			    	}else{
			    		 //向服务端发送删除指令
			    		$.post('/stock/inbound/celview?id='+data.id,{},function(result) {
			    			if(result.code == 200) {
			    				//obj.del(); //删除对应行（tr）的DOM结构
// 			    				obj.update({
// 									status:0			    				
// 			    				});
							    layer.msg(result.message,{icon: 6});
							    reloadData();
								layer.close(index);
// 							    ins.reload({
// 			           	    		url:'/stock/inbound/list',
// 			           	    		where: {
// 			           	    			companyId: '1'
// 			           	    			//reviewStatus: '0'
// 			           	    		}
// 			           	    	});
							    return false;
			    			}else{
			    			 layer.msg(result.message,{icon: 2});
			    			 return false;
			    			} 
			    		});
			    	}
				});
				
			}else if(layEvent=="review") {
				console.log(data.status);
				layer.confirm('确定审核这条单据', {icon: 3, title:'提示'},function(index) {
					if(data.status == 1) {
			    		layer.msg("单据已审核，不能审核！！", {icon: 4});
			    		layer.close(index);
			    	}else{
			    		 //向服务端发送删除指令
			    		$.post('/stock/inbound/review?id='+data.id,{},function(result) {
			    			if(result.code == 200) {
			    				//obj.del(); //删除对应行（tr）的DOM结构
			    				/* obj.update({
									status:0			    				
			    				}); */
			    				reloadData();
							    layer.msg(result.message,{icon: 6});
							    return false;
			    			}else{
			    			 layer.msg(result.message,{icon: 2});
			    			 return false;
			    			} 
			    		});
			    		layer.close(index);
			    	}
				});
			}else if(layEvent=="detail"){
				console.log("hello");				
				//  var name=$(".tableColumnCheckbox:checked").parent(".sorting_1").next().next().next().html();
	                var url="/stock/inbound/detail/index?id="+data.id;
	                menuOpen(url,"采购入库单【"+data.orderNo+"】的明细");	
			
				//location.href="/stock/inbounddetail/index/1";
			}
		});
		//表格重载，确定当前的页按钮
		function reloadData(){
			$(".layui-laypage-btn")[0].click();//页面的确定按钮
			
		}
		
		$("#searchBtn").click(function(){
			var phone = $("input[name='phone']").val();
			var table = layui.table;
		    ins.reload({url: '/stock/inbound/data',where:$("#searchForm").serializeJson()})
		    return false;
		});
		//采购入库单新增	
		$("#addBtn").click(function() {
			var companyid = $("#companyId option:selected").val();
			console.log(companyid)
			$.post('/stock/inbound/addinit?companyId='+companyid,{},function(str) {
// 			$.post('/stock/inbound/add',$("#searchForm").serializeJson(),function(str) {
			editdialogindex = layer.open({
					title:"新增采购入库单",
					type:1,
					area:['800px', '500px'],
					content:str,
					zIndex:10
				});
			});
		});
	});
		
	layui.use("laydate",function(){
		var laydate = layui.laydate;
		laydate.render({
			  elem: '#startDate'//指定元素
			  
			  ,done: function (value,date,enddate){
				  console.log(value);
				  console.log(date);
				  console.log(enddate);
			  }
			});
		laydate.render({
			  elem: '#endDate' //指定元素
			  ,done: function(value,date,enddate){
				console.log(value);				 
			  }
			});
	});
}); 

</script>
<script type="text/html" id="statusTpl">
{{#if (d.status==0){}}
  		未审核
{{# }else if(d.status==1) {}}
		审核
{{# }else if(d.status==2) {}}
		关闭
{{# }else if(d.status==3) {}}
		完成
{{#  } }}
</script>
<script type="text/html" id="taxTpl">
{{#if (d.taxRate !=null && d.taxRate!=''){}}
<span>{{d.taxRate}}%</span>
{{# }}}
</script>
<script type="text/html" id="toolbar">
	<a class="layui-btn  layui-btn-mini" lay-event="view">查看</a>
	<a class="layui-btn layui-btn-mini" lay-event="detail">明细</a>
	{{#if (d.status==0){}}
		<a class="layui-btn layui-btn-mini" lay-event="edit" authcode="900036">修改</a>
		<a class="layui-btn layui-btn-mini" lay-event="review" href="javascript:void(0)" authcode="900033">审核</a>
		<a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="del" authcode="900032">删除</a>
		<a class="layui-btn layui-bg-orange layui-btn-mini" lay-event="close" authcode="900035">关闭</a>
	{{# }else if(d.status==1){}}
		<a class="layui-btn layui-btn-mini" lay-event="celview" href="javascript:void(0)" authcode="900034">弃审</a>
	{{# }}}
</script>
<!--<div th:replace="/department/model::modelDiv"></div>-->
</th:block>
</body>
</html>